package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Cart;
import csu.web.mypetstore.persistence.AccountDao;
import csu.web.mypetstore.persistence.impl.AccountDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class
CartFormServlet extends HttpServlet {
    private static final String CART_FORM="/WEB-INF/jsp/cart/cart.jsp";
    private static final String MAIN_FORM="/WEB-INF/jsp/catalog/main.jsp";
    private static final String NULL_FORM="/WEB-INF/jsp/common/cartnull.jsp";
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session = req.getSession();
        Cart cart = (Cart) session.getAttribute("cart");
        /*String cartMsg="";*/

        AccountDao accountDao = new AccountDaoImpl();
        Account loginAccount = (Account) session.getAttribute("loginAccount");

        if (!accountDao.selectShoppingcartByUsernameAndTimes(loginAccount)) {
            req.getRequestDispatcher(NULL_FORM).forward(req, resp);
        }else{
            /*cartMsg="You have one cart,please update your shopping cart,or your inf may lose";
            session.setAttribute("cartMsg",cartMsg);*/
            session.setAttribute("cart",cart);
            req.getRequestDispatcher(CART_FORM).forward(req, resp);
        }
    }
}
